package com.llkj.youdaocar;

import android.app.ActivityManager;
import android.app.Application;
import android.app.Notification;
import android.content.Context;
import android.content.Intent;
import android.os.Handler;
import android.text.TextUtils;
import android.util.Log;
import android.widget.RemoteViews;
import android.widget.Toast;

import com.google.gson.JsonObject;
import com.llkj.youdaocar.push.UmengNotificationService;
import com.llkj.youdaocar.utils.SpUtil;
import com.mob.MobSDK;
import com.umeng.analytics.MobclickAgent;
import com.umeng.commonsdk.UMConfigure;
import com.umeng.message.IUmengRegisterCallback;
import com.umeng.message.MsgConstant;
import com.umeng.message.PushAgent;
import com.umeng.message.UTrack;
import com.umeng.message.UmengMessageHandler;
import com.umeng.message.UmengNotificationClickHandler;
import com.umeng.message.entity.UMessage;

import org.android.agoo.huawei.HuaWeiRegister;
import org.android.agoo.xiaomi.MiPushRegistar;
import org.json.JSONException;
import org.json.JSONObject;

import anet.channel.util.StringUtils;

/**
 * Created by Administrator on 2018/6/6.
 */

public class MyApplication extends Application {

    private static MyApplication application;
    private SpUtil spUtil;
    private String deviceToken;
    private Handler handler;

    @Override
    public void onCreate() {
        super.onCreate();
        application = this;
        spUtil = SpUtil.init(this);

        // ShareSDK
        MobSDK.init(this, "264634172ae28", "01a23ee76511335f904562f6e435e146");
        initUMPush();
    }

    public static MyApplication getInstance(){
        return application;
    }

    private void initUMPush(){
        UMConfigure.init(this,"5b582372b27b0a4b590000d7", "Umeng", UMConfigure.DEVICE_TYPE_PHONE, "6f3916b114f00f1b273c9e3baaa5b2c8");
        MobclickAgent.setScenarioType(this, MobclickAgent.EScenarioType.E_UM_NORMAL);

        MiPushRegistar.register(this,"2882303761517832231","5671783251231");
        HuaWeiRegister.register(this);

        PushAgent mPushAgent = PushAgent.getInstance(this);
        handler = new Handler(getMainLooper());
        //sdk开启通知声音
        mPushAgent.setNotificationPlaySound(MsgConstant.NOTIFICATION_PLAY_SDK_ENABLE);
        mPushAgent.setNotificationPlayVibrate(MsgConstant.NOTIFICATION_PLAY_SERVER);//振动

        //注册推送服务，每次调用register方法都会回调该接口
        mPushAgent.register(new IUmengRegisterCallback() {
            @Override
            public void onSuccess(String deviceToken) {
                //注册成功会返回device token
                Log.e("Log","====="+deviceToken);
                application.deviceToken = deviceToken;
            }
            @Override
            public void onFailure(String s, String s1) {
            }
        });
        mPushAgent.setPushIntentServiceClass(UmengNotificationService.class);

//        mPushAgent.setPushIntentServiceClass(null);
        /**
         * 自定义行为的回调处理，参考文档：高级功能-通知的展示及提醒-自定义通知打开动作
         * UmengNotificationClickHandler是在BroadcastReceiver中被调用，故
         * 如果需启动Activity，需添加Intent.FLAG_ACTIVITY_NEW_TASK
         * */
//        UmengNotificationClickHandler notificationClickHandler = new UmengNotificationClickHandler() {
//
//            @Override
//            public void launchApp(Context context, UMessage msg) {
//                super.launchApp(context, msg);
//            }
//
//            @Override
//            public void openUrl(Context context, UMessage msg) {
//                super.openUrl(context, msg);
//            }
//
//            @Override
//            public void openActivity(Context context, UMessage msg) {
//                super.openActivity(context,msg);
//            }
//
//            @Override
//            public void dealWithCustomAction(Context context, UMessage msg) {
//                //处理点击通知跳转界面
////                if(!TextUtils.isEmpty(msg.custom)){
//                    String linkUrl = msg.extra.get("linkUrl");
//                    Intent intentAction = new Intent(MyApplication.getInstance(),WebViewActivity.class);
//                    intentAction.putExtra("url",linkUrl);
//                    intentAction.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
//                    MyApplication.getInstance().startActivity(intentAction);
////                }
//
//            }
//        };
//        //使用自定义的NotificationHandler
//        mPushAgent.setNotificationClickHandler(notificationClickHandler);
//
//        UmengMessageHandler messageHandler = new UmengMessageHandler() {
//
//            /**
//             * 通知的回调方法（通知送达时会回调）
//             */
//            @Override
//            public void dealWithNotificationMessage(Context context, UMessage msg) {
//                //调用super，会展示通知，不调用super，则不展示通知。
//                super.dealWithNotificationMessage(context, msg);
//            }
//        };
//        mPushAgent.setMessageHandler(messageHandler);
    }

    @Override
    public void onTerminate() {
        Log.e("Log","====onTerminate()=====");
        super.onTerminate();
        application = null;
    }
    @Override
    protected void attachBaseContext(Context base) {
        super.attachBaseContext(base);
//        MultiDex.install(this);
    }

    public SpUtil getSpUtil(){
        return spUtil;
    }

    public String getDeviceToken(){
        return deviceToken;
    }

    public boolean isMainProcess() {
        int pid = android.os.Process.myPid();
        ActivityManager activityManager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
        for (ActivityManager.RunningAppProcessInfo appProcess : activityManager.getRunningAppProcesses()) {
            if (appProcess.pid == pid) {
                return getApplicationInfo().packageName.equals(appProcess.processName);
            }
        }
        return false;
    }
}
